<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/09/15 22:17:02 $)
// $RCSfile: staff_canned.php,v $ : $Revision: 1.12 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["ls_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./includes/functions_html.php");
require_once ("./modules/livesupport/functions_canned.php");
$grid = new Grid();
$template->loadLanguageSection("livesupport2");

$interface->assignQuickLink($_SWIFT["language"]["managecategories"], "index.php?_m=livesupport&_a=canned", "icon_list.gif");
$interface->assignQuickLink($_SWIFT["language"]["manageresponses"], "index.php?_m=livesupport&_a=managecannedresponses", "icon_topicyellow.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertcategory"], "index.php?_m=livesupport&_a=insertcannedcategory", "icon_newcategory.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertresponse"], "index.php?_m=livesupport&_a=insertcannedresponse", "icon_inserttopic.gif");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing response
*/
function _responseFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["cannedresponses.responsetype"] = $_SWIFT["language"][$record["responsetype"]];
	$record["cannedresponses.title"] = '<a href="index.php?_m=livesupport&_a=editcannedresponse&cannedresponseid='. $record["cannedresponseid"] .'" title="'. $_SWIFT["language"]["edit"] .'">'.$record["title"].'</a>';

	$record["options"] = '<a href="index.php?_m=livesupport&_a=editcannedresponse&cannedresponseid='. $record["cannedresponseid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=livesupport&_a=managecannedresponses&do=delete&cannedresponseid='. $record["cannedresponseid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}

/**
* Mass Action: Delete Responses
*/
function _maDeleteResponses()
{
	global $_SWIFT, $departments;

	if (!is_array($_POST["itemid"]))
	{
		return false;
	}

	deleteResponses($_POST["itemid"]);
}




/**
* ###############################################
* Manage Canned Categories
* ###############################################
*/
if ($eventaction == "canned")
{
	if ($_GET["do"] == "delete" && $_GET["cannedcategoryid"] != "")
	{
		deleteCategory($_GET["cannedcategoryid"]);

		printRedirect($_SWIFT["language"]["redirect_catdel"], "index.php?_m=livesupport&_a=canned&categorydel=1");
		exit;
	}

	if ($_REQUEST["newcatinsert"] == 1)
	{
		$infomessage = $_SWIFT["language"]["catinsertconfirm"];
	} else if ($_REQUEST["categorydel"] == 1) {
		$infomessage = $_SWIFT["language"]["catdelconfirm"];
	} else if ($_REQUEST["catupdate"] == 1) {
		$infomessage = $_SWIFT["language"]["catupdateconfirm"];
	}
	
	require_once ("./includes/TreeMenu/TreeMenu.php");
	$template->assign("treemenu", true);

	$interface->staffHeader($_SWIFT["language"]["canned"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=canned" title="'.$_SWIFT["language"]["canned"].'">'.$_SWIFT["language"]["canned"]."</a>", "", 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);
	
	$icon         = 'icon_folderyellow3.gif';
	$expandedIcon = 'icon_folderyellow3.gif';

	$menu  = new HTML_TreeMenu();

	$node1   = new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["parentcategory"] ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='index.php?_m=livesupport&_a=insertcannedcategory&parentcategoryid=0' id='blue' title='". $_SWIFT["language"]["addsubcat"] ."'>". $_SWIFT["language"]["addsubcat"] ."</a> | <a href='index.php?_m=livesupport&_a=insertcannedresponse&parentcategoryid=0' id='blue' title='". $_SWIFT["language"]["addresponse"] ."'>". $_SWIFT["language"]["addresponse"] ."</a> ]", 'link' => "", 'icon' => "icon_list.gif", 'expandedIcon' => "icon_list.gif", 'expanded' => true));

	buildCategoryTreeData($node1);

	$menu->addItem($node1);
	
	// Create the presentation class
	$treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => stripTrailingSlash($_SWIFT["themepath"]), 'defaultClass' => 'treeMenuDefault'));
	$treeMenu->userPersistence = true;

	printMainTableHeader($_SWIFT["language"]["cannedcategories"]);

	ob_start();
	$treeMenu->printMenu();
	$contents = ob_get_contents();
	ob_end_clean();

	printDescRow($contents, "2", "defaultrow");

	printMainTableFooter();

	$interface->staffFooter();





/**
* ###############################################
* Manage Responses
* ###############################################
*/
} else if ($eventaction == "managecannedresponses") {
	if ($_GET["do"] == "delete" && $_GET["cannedresponseid"] != "")
	{
		$infomessage = $_SWIFT["language"]["responsedelconfirm"];
		deleteResponses(array($_GET["cannedresponseid"]));
	}

	if (trim($_REQUEST["newresponseinsert"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["responseinsertconfirm"], $_REQUEST["newresponseinsert"]);
	} else if ($_REQUEST["responseupdate"] == "1") {
		$infomessage = $_SWIFT["language"]["responseupdateconfirm"];
	}
	

	$cannedcategories = getSubCategoryList("0", buildCategoryData(($_temp=array())), ($_temp = array()));
	$cannedcategories[] = 0;

	$options["recordsperpage"] = "12";
	$options["sortby"] = "cannedresponses.title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteResponses";

	$options["idname"] = "cannedresponseid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'cannedresponses` AS cannedresponses WHERE cannedresponses.title LIKE \'%$_searchstr%\' AND cannedresponses.cannedcategoryid IN ('. buildIN($cannedcategories) .') $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["contents"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'cannedresponses` AS  cannedresponses LEFT JOIN `'. TABLE_PREFIX .'cannedresponsedata` AS cannedresponsedata ON (cannedresponses.cannedresponseid = cannedresponsedata.cannedresponseid) WHERE cannedresponsedata.contents LIKE \'%$_searchstr%\' AND cannedresponses.cannedcategoryid IN ('. buildIN($cannedcategories) .') $_sortjoin;';

	$options["callback"] = "_responseFields";

	$fields[0]["name"] = "cannedresponses.title";
	$fields[0]["title"] = $_SWIFT["language"]["title"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "cannedresponses.responsetype";
	$fields[1]["title"] = $_SWIFT["language"]["responsetype"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["type"] = "custom";
	$fields[2]["name"] = "options";
	$fields[2]["title"] = $_SWIFT["language"]["options"];
	$fields[2]["width"] = "220";
	$fields[2]["align"] = "center";

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["cannedresponses"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=canned" title="'.$_SWIFT["language"]["categories"].'">'.$_SWIFT["language"]["categories"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=managecannedresponses" title="'.$_SWIFT["language"]["cannedresponses"].'">'.$_SWIFT["language"]["cannedresponses"]."</a>", "", 3);

	printInfoBox($infomessage);

	$grid->start("cannedresponses", $_SWIFT["language"]["responselist"], 'SELECT * FROM `'. TABLE_PREFIX .'cannedresponses` AS cannedresponses WHERE cannedresponses.cannedcategoryid IN ('. buildIN($cannedcategories) .') $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'cannedresponses` AS cannedresponses WHERE cannedresponses.cannedcategoryid IN ('. buildIN($cannedcategories) .');', 'SELECT * FROM `'. TABLE_PREFIX .'cannedresponses` AS cannedresponses WHERE cannedresponses.title LIKE \'%$_searchstr%\' AND cannedresponses.cannedcategoryid IN ('. buildIN($cannedcategories) .') $_sortjoin;', $fields, $options);
	
	$grid->display("cannedresponses");

	$interface->staffFooter();






/**
* ###############################################
* Add New Canned Category
* ###############################################
*/
} else if ($eventaction == "insertcannedcategory") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			// Insert the new category

			addCategory($_POST["parentcategoryid"], $_POST["title"], iif($_POST["type"]==1, SWIFTPUBLIC, SWIFTPRIVATE));

			printRedirect($_SWIFT["language"]["redirect_catinsert"], "index.php?_m=livesupport&_a=canned&newcatinsert=1");
			exit;
		}		
	}

	$interface->staffHeader($_SWIFT["language"]["insertcategory"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=canned" title="'.$_SWIFT["language"]["categories"].'">'.$_SWIFT["language"]["categories"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=insertcannedcategory" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"]."</a>", "", 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCategoryForm(INSERT);

	$template->assign("backurl", "index.php?_m=livesupport&_a=canned");
	$interface->staffFooter();









/**
* ###############################################
* Add Canned Response
* ###############################################
*/
} else if ($eventaction == "insertcannedresponse") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			addResponse($_POST["parentcategoryid"], $_POST["responsetype"], $_POST["title"], $_POST["contents"]);

			printRedirect(sprintf($_SWIFT["language"]["redirect_responseinsert"], $_POST["title"]), "index.php?_m=livesupport&_a=managecannedresponses&newresponseinsert=".urlencode($_POST["title"]));
			exit;
		}		
	}


	$interface->staffHeader($_SWIFT["language"]["insertresponse"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=managecannedresponses" title="'.$_SWIFT["language"]["cannedresponses"].'">'.$_SWIFT["language"]["cannedresponses"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=insertcannedresponse" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"]."</a>", "", 3);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderResponseForm(INSERT);

	$template->assign("backurl", "index.php?_m=livesupport&_a=managecannedresponses");
	$interface->staffFooter();





/**
* ###############################################
* Edit Canned Category
* ###############################################
*/
} else if ($eventaction == "editcannedcategory") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_POST["parentcategoryid"] == $_POST["cannedcategoryid"]) {
			$errormessage = $_SWIFT["language"]["cannotsetparenttoself"];
		} else {
			updateCategory($_POST["cannedcategoryid"], $_POST["parentcategoryid"], $_POST["title"], iif($_POST["type"]==1, SWIFTPUBLIC, SWIFTPRIVATE));

			printRedirect($_SWIFT["language"]["redirect_catupdate"], "index.php?_m=livesupport&_a=canned&catupdate=1");
			exit;
		}		
	}

	$_category = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."cannedcategories` WHERE `cannedcategoryid` = '". $dbCore->escape($_REQUEST["cannedcategoryid"]) ."';");
	if (trim($_category["cannedcategoryid"]) == "")
	{
		trigger_error($_SWIFT["language"]["invalidcategory"], E_USER_ERROR);
	}
	$_POST["title"] = $_category["title"];
	$_REQUEST["parentcategoryid"] = $_category["parentcategoryid"];
	$_POST["type"] = $_category["categorytype"];

	$template->assign("itemoptiontitle", $_SWIFT["language"]["categoryoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deletecategory"], "", "icon_delete.gif", "", "doConfirm('". $_SWIFT["language"]["catdelactionconfirm"] ."', 'index.php?_m=livesupport&_a=canned&do=delete&cannedcategoryid=". $_category["cannedcategoryid"] ."')");

	$interface->staffHeader($_SWIFT["language"]["editcategory"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=canned" title="'.$_SWIFT["language"]["categories"].'">'.$_SWIFT["language"]["categories"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=editcannedcategory&cannedcategoryid='. intval($_category["cannedcategoryid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"]."</a>", "", 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCategoryForm(EDIT);

	$template->assign("backurl", "index.php?_m=livesupport&_a=canned");
	$interface->staffFooter();







/**
* ###############################################
* Edit Canned Response
* ###############################################
*/
} else if ($eventaction == "editcannedresponse") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			updateResponse($_POST["cannedresponseid"], $_POST["parentcategoryid"], $_POST["responsetype"], $_POST["title"], $_POST["contents"]);

			printRedirect($_SWIFT["language"]["responseupdateconfirm"], "index.php?_m=livesupport&_a=managecannedresponses&responseupdate=1");
			exit;
		}		
	}

	$_response = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."cannedresponses` AS cannedresponses LEFT JOIN `". TABLE_PREFIX ."cannedresponsedata` AS cannedresponsedata ON (cannedresponses.cannedresponseid = cannedresponsedata.cannedresponseid) WHERE cannedresponses.cannedresponseid = '". $dbCore->escape($_REQUEST["cannedresponseid"]) ."';");
	if (trim($_response["cannedresponseid"]) == "")
	{
		trigger_error($_SWIFT["language"]["invalidresponse"], E_USER_ERROR);
	}

	$_POST["title"] = $_response["title"];
	$_REQUEST["parentcategoryid"] = $_response["cannedcategoryid"];
	$_POST["type"] = $_response["responsetype"];
	$_POST["contents"] = $_response["contents"];
	$_POST["responsetype"] = $_response["responsetype"];

	$template->assign("itemoptiontitle", $_SWIFT["language"]["responseoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deleteresponse"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=livesupport&_a=managecannedresponses&do=delete&cannedresponseid='. $_response["cannedresponseid"] .'\')');

	$interface->staffHeader($_SWIFT["language"]["editresponse"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=managecannedresponses" title="'.$_SWIFT["language"]["cannedresponses"].'">'.$_SWIFT["language"]["cannedresponses"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=editcannedresponse&cannedresponseid='. intval($_response["cannedresponseid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"]."</a>", "", 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderResponseForm(EDIT);

	$template->assign("backurl", "index.php?_m=livesupport&_a=managecannedresponses");
	$interface->staffFooter();

}
?>